﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;

namespace GIVisualEditor
{
    [Serializable()]
    public class Edge
    {
        private Node startNode = null;
        private Node endNode = null;
        private int bindingPosIndex = 0;
        private StateConnector view = null;
        private DirectedGraph graph = null;

        [Browsable(false)]
        public StateConnector View
        {
            get { return view; }
            set { view = value; }
        }

        [Browsable(false)]
        public int BindingPosIndex
        {
            get { return bindingPosIndex; }
            set { bindingPosIndex = value; }
        }

        [CategoryAttribute("Edge"), DescriptionAttribute("Index to the starting state.")]
        public Node FromState
        {
            get { return startNode; }
            set { startNode = value; }
        }

        [CategoryAttribute("Edge"), DescriptionAttribute("Index to the end state.")]
        public Node ToState
        {
            get { return endNode; }
            set { endNode = value; }
        }


        public Edge(DirectedGraph g)
        {
            this.graph = g;
        }

        public Edge(DirectedGraph g, Node N1, Node N2)
        {
            this.graph = g;
            this.startNode = N1;
            this.endNode = N2;
            this.view = new StateConnector(graph, this);
        }

        public void ToXML(ref XmlWriter oWriter)
        {
            oWriter.WriteStartElement("Edge");
            int iStartIndex = graph.Nodes.FindIndex(X => X == startNode),
                iEndIndex = graph.Nodes.FindIndex(X => X == endNode);
            DirectedGraph.WriteNodeVale("FromNode", iStartIndex.ToString(), ref oWriter);
            DirectedGraph.WriteNodeVale("ToNode", iEndIndex.ToString(), ref oWriter);
            oWriter.WriteEndElement();
        }
    }
}
